package com.rfid.api;

public class GFunction {
    private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    public static char[] encodeHex(byte[] data) {
        return encodeHex(data, true);
    }

    public static char[] encodeHex(byte[] data, int dataLen) {
        return encodeHex(data, dataLen, true);
    }

    public static char[] encodeHex(byte[] data, boolean toLowerCase) {
        return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
    }

    public static char[] encodeHex(byte[] data, int dataLen, boolean toLowerCase) {
        return encodeHex(data, dataLen, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
    }

    protected static char[] encodeHex(byte[] data, char[] toDigits) {
        int len = data.length;
        char[] out = new char[len << 1];
        for (int i = 0, j = 0; i < len; i++) {
            out[j++] = toDigits[(0xF0 & data[i]) >>> '\004'];
            out[j++] = toDigits[0xF & data[i]];
        }
        return out;
    }

    protected static char[] encodeHex(byte[] data, int dataLen, char[] toDigits) {
        int len = dataLen;
        char[] out = new char[len << 1];
        for (int i = 0, j = 0; i < len; i++) {
            out[j++] = toDigits[(0xF0 & data[i]) >>> '\004'];
            out[j++] = toDigits[0xF & data[i]];
        }
        return out;
    }

    public static String encodeHexStr(byte[] data) {
        return encodeHexStr(data, false);
    }

    public static String encodeHexStr(byte[] data, int dataLen) {
        return encodeHexStr(data, dataLen, false);
    }

    public static String encodeHexStr(byte[] data, boolean toLowerCase) {
        return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
    }

    public static String encodeHexStr(byte[] data, int dataLen, boolean toLowerCase) {
        return encodeHexStr(data, dataLen, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
    }

    protected static String encodeHexStr(byte[] data, char[] toDigits) {
        return new String(encodeHex(data, toDigits));
    }

    protected static String encodeHexStr(byte[] data, int dataLen, char[] toDigits) {
        return new String(encodeHex(data, dataLen, toDigits));
    }

    public static byte[] decodeHex(char[] data) {
        int len = data.length;
        if ((len & 0x1) != 0) return null;
        byte[] out = new byte[len >> 1];
        for (int i = 0, j = 0; j < len; i++) {
            int f1 = toDigit(data[j], j) << 4;
            if (f1 < 0) return null;
            j++;
            int f2 = toDigit(data[j], j);
            if (f2 < 0) return null;
            j++;
            out[i] = (byte) ((f1 | f2) & 0xFF);
        }
        return out;
    }

    public static byte[] decodeHex(char[] data, int dataLen) {
        int len = dataLen;
        if ((len & 0x1) != 0) return null;
        byte[] out = new byte[len >> 1];
        for (int i = 0, j = 0; j < len; i++) {
            int f1 = toDigit(data[j], j) << 4;
            if (f1 < 0) return null;
            j++;
            int f2 = toDigit(data[j], j);
            if (f2 < 0) return null;
            j++;
            out[i] = (byte) ((f1 | f2) & 0xFF);
        }
        return out;
    }

    public static byte[] decodeHex(String sData) {
        if (sData.equals("")) return null;
        char[] data = sData.toCharArray();
        int len = data.length;
        if ((len & 0x1) != 0) return null;
        byte[] out = new byte[len >> 1];
        for (int i = 0, j = 0; j < len; i++) {
            int t1 = toDigit(data[j], j) << 4;
            if (t1 < 0) return null;
            j++;
            int t2 = toDigit(data[j], j);
            if (t2 < 0) return null;
            j++;
            out[i] = (byte) ((t1 | t2) & 0xFF);
        }
        return out;
    }

    protected static int toDigit(char ch, int index) {
        return Character.digit(ch, 16);
    }

    public static void BytesReverse(byte[] buf, int len) {
        for (int i = 0; i < len / 2; i++) {
            byte tmp = buf[i];
            buf[i] = buf[len - 1 - i];
            buf[len - 1 - i] = tmp;
        }
    }
}
